const CREDS = 'include'
function checkStatus (response) {
  if (response.status >= 200 && response.status < 300) { return response }
  const error = new Error(response.statusText)
  error.response = response
  throw error
}
const asyncFetch = async function (obj) {
  const url = obj.url
  const method = obj.method || 'GET'
  const credentials = obj.credentials || CREDS
  const body = obj.obj || null
  const headers = {
    'content-type': 'application/x-www-form-urlencoded',
    'qbtoken': obj.qbtoken
  }
  if (process.env.NODE_ENV === 'development') {
    headers.qbtoken = '7c89dfe55f6d23868d8063dc042a5737541de579'
  }
  let confFetch = { headers, method, credentials }
  if (method === 'POST') { confFetch = { method, credentials, body: JSON.stringify(body) } }
  return new Promise(function (resolve, reject) {
    fetch(url, confFetch)
      .then(checkStatus)
      .then(res => res.json())
      .then(res => { resolve(res) })
      .catch(err => { reject(err) })
  })
}

export default asyncFetch

